package com.hxf.web.action;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

import javax.annotation.Resource; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
   
import com.hxf.common.GlobalConstant;
import com.hxf.dto.UserDto;
import com.hxf.service.facade.ServiceFacade;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; 

/**
 * 公共Action
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public abstract class WebActionBase extends ActionSupport implements ModelDriven<Object>{

	private static final long serialVersionUID = 1L;

	protected final org.apache.log4j.Logger logger = Logger.getLogger(getClass());
	@Resource
	protected ServiceFacade serviceFacade;
	
	protected Integer targetPage = 1; 
	protected Integer pageSize=20; 
	protected Integer recordCount; 
	protected Integer pageCount; 
	protected String requestURL;
	protected String webUrl;
	protected String msg;
	protected String json;
	protected String urlType;
	protected String resultType;
	
	protected String lpUrl;
	
	public String getUrlType() {
		return urlType;
	}


	public void setUrlType(String urlType) {
		this.urlType = urlType;
	}


	public String getResultType() {
		return resultType;
	}


	public void setResultType(String resultType) {
		this.resultType = resultType;
	}

	@Override
	public Object getModel() {
		 
		return null;
	}

	  
	public Integer getPageCount() {
		return pageCount;
	}

	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}

	public String getRequestURL() {
		return requestURL;
	}

	public void setRequestURL(String requestURL) {
		this.requestURL = requestURL;
	}

	public HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	public void setAttribute(String key, Object value) {
		getRequest().setAttribute(key, value);
	}
	
	public void println(Object args) throws IOException {
		getResponse().getWriter().println(args);
	}
 
	public ServiceFacade getServiceFacade() {
		return serviceFacade;
	}

	public void setServiceFacade(ServiceFacade serviceFacade) {
		this.serviceFacade = serviceFacade;
	}

	public String getWebUrl() { 
		return getRequest().getScheme()+"://"+getRequest().getServerName()+":"+getRequest().getServerPort() + "/"+ GlobalConstant.PROJECT_API;
	}
	
	public String getParameter(String args) {
		return getRequest().getParameter(args);
	}

	public UserDto getUser() { 
		return (UserDto)getSession().getAttribute("userDto");
	}  
	public String getMsg() {
		return msg;
	}
 
	public void setWebUrl(String webUrl) {
		this.webUrl = webUrl;
	} 
 
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	
	public Integer getTargetPage() {
		targetPage = targetPage == null || targetPage <= 0 ? 1 : targetPage;
		return targetPage;
	}

	public void setTargetPage(Integer targetPage) {
		targetPage = targetPage == null || targetPage <= 0 ? 1 : targetPage;
		this.targetPage = targetPage;
	}

	public Integer getPageSize() {
		if (pageSize == null || pageSize <= 0) {
			pageSize = 20;
		}
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getRecordCount() {
		return recordCount;
	}

	public void setRecordCount(Integer recordCount) {
		this.recordCount = recordCount;

		// pageCount = pageCount == null ? 0 : pageCount;
		int page = this.recordCount % pageSize;
		if (page == 0) {
			pageCount = this.recordCount / pageSize;
		} else {
			pageCount = this.recordCount / pageSize + 1;
		} 
	}
	
	public String gzip(File file) throws IOException { 
		byte [] result = null; 
		    // System.out.println("Before compression, the data size is ："+ result.length);  
		    // Using gzip compress the data  
		   FileInputStream fin = new FileInputStream(file);
		   
		    ByteArrayOutputStream out = new ByteArrayOutputStream();  
		    GZIPOutputStream gout = new GZIPOutputStream(out);   
		    //设定读入缓冲区尺寸   
            byte[] buf = new byte[1024];  
            int num;   
            while ((num = fin.read(buf)) != -1)   
            {   
                gout.write(buf,0,num);   
            }   
            gout.close();
            fin.close();
            result = out.toByteArray();
		    System.out.println(result);
		    getResponse().setContentType("text/gzip; charset=UTF-8");
		    getResponse().setHeader("Content-Encoding","gzip");  
		    getResponse().setHeader("Content-Length", result.length+"");   
		    getResponse().getOutputStream().write(result); 
		return null;
	}
	
	public String gzip(String str) throws IOException {
		String json = str;
		byte [] result = null; 
	    // System.out.println("Before compression, the data size is ："+ result.length);  
	    // Using gzip compress the data    
	    ByteArrayOutputStream out = new ByteArrayOutputStream();  
	    GZIPOutputStream gout = new GZIPOutputStream(out); 
	    gout.write(json.getBytes("UTF-8"));  
	    gout.close();  
	    result = out.toByteArray();  
	    
	    System.out.println(result);
	    getResponse().setContentType("text/gzip; charset=UTF-8");
	    getResponse().setHeader("Content-Encoding","gzip");  
	    getResponse().setHeader("Content-Length", result.length+"");   
	    getResponse().getOutputStream().write(result); 
		return null;
	}


	public String getJson() {
		return json;
	}


	public void setJson(String json) {
		this.json = json;
	}


	public String getLpUrl() {
		return "lp://"+getRequest().getServerName()+":"+getRequest().getServerPort() + "/"+ GlobalConstant.PROJECT_API;
	}


	public void setLpUrl(String lpUrl) {
		this.lpUrl = lpUrl;
	}
	
}
